###############################################################################

# Replication File (Observational): "The Effect of Simultaneous Proposals"
# Klaudia Wegschaider, February 2025

###############################################################################

# STRUCTURE

# This file covers the replication of the observational cantonal data.
# For the replication of the experimental data, please see the other file.

# In the first part, I will cover all the analyses that are included in
# the main text of the paper.

# In the second part, I will cover all the analyses that are included in the
# appendices.

# I do not explain the purpose of all the steps here. Readers should consult
# this file in combination with the manuscript and appendix.

###############################################################################

# BACKGROUND

# This code is based on R version 4.1.3 (2022-03-10)

# Please load the required packages.

library(kableExtra)
library(modelsummary)
library(tidyr)
library(dplyr)
library(plm)
library(tibble)
library(lmtest)

# sessionInfo()
# R version 4.1.3 (2022-03-10)
# other attached packages:
# [1] lmtest_0.9-40      zoo_1.8-12         tibble_3.2.1       plm_2.6-1         
# [5] dplyr_1.1.2        tidyr_1.3.0        modelsummary_1.0.2 kableExtra_1.4.0 

###############################################################################

# CANTONAL DATA 

# Full cantonal dataset (contains data at cantonal level)
data <- read.csv(file="cantonal_dataset.csv") 
summary(data)
View(data)

# Subsetting for subsequent analyses (removing extreme versions in pairs)
data_partial <- data[(data$Extreme==0),] 
summary(data_partial)

# Subsetting for subsequent analyses (removing moderate versions in pairs)
data_partial_p <- data[(data$Simultaneous==0),]
summary(data_partial_p)

###############################################################################

# PART ONE: ANALYSES/RESULTS INCLUDED IN THE MAIN TEXT

# ANALYSIS OF CANTONAL DATA (MAIN HYPOTHESIS)

# All analyses are run with the partial dataset (extreme versions removed).

## What part of the variation is explained by the simultaneous vote?
# random effects, no controls, full dataset
model1a <- lm(data_partial$Pro ~ data_partial$Simultaneous)
summary(model1a)

## What part of the variation is explained by the simultaneous vote?
# random effects, no controls, full dataset, added inclusion control
model1b <- lm(data_partial$Pro ~ data_partial$Simultaneous + data_partial$Scale)
summary(model1b)

## What part of the variation is explained by the simultaneous vote?
# random effects, no controls, full dataset
model1c <- lm(data_partial$Pro ~ data_partial$Simultaneous + data_partial$Scale + data_partial$Experience)
summary(model1c)

## What part of the variation is explained by the simultaneous vote?
model1d <- lm(data_partial$Pro ~ data_partial$Simultaneous + data_partial$Scale + data_partial$Experience + data_partial$CantonalLeft)
summary(model1d)

# Making the table 
cant_models_h <- list(model1a, model1b, model1c, model1d)

cm1 <- c('data_partial$Simultaneous'    = 'Simultaneous moderate, $M_{Sim}$ \\\\ (baseline: non-simultaneous)',
         'data_partial$Scale'    = 'Inclusiveness (range: 1-6)',
         'data_partial$Experience' = 'Prior experience',
         'data_partial$CantonalLeft' = 'Cantonal left')

table1 <- modelsummary(cant_models_h, output = 'kableExtra',
                       fmt = 1, 
                       stars = c('*' = .1, '**' = .05, '***' = .01),
                       coef_omit = "Intercept",
                       coef_map = cm1,
                       gof_map = c("nobs"),
                       escape = FALSE,
                       title = 'Simultaneous Votes in Swiss Referendums (Hypothesis 1)')
table1

###

# ANALYSIS OF CANTONAL DATA (ROBUSTNESS CHECK)

## What part of the variation is explained by the simultaneous vote?
model1a_p1 <- lm(data_partial_p$Pro ~ data_partial_p$Extreme)
summary(model1a_p1)

## What part of the variation is explained by the simultaneous vote?
model1b_p1 <- lm(data_partial_p$Pro ~ data_partial_p$Extreme + data_partial_p$Scale)
summary(model1b_p1)

## What part of the variation is explained by the simultaneous vote?
model1c_p1 <- lm(data_partial_p$Pro ~ data_partial_p$Extreme + data_partial_p$Scale + data_partial_p$Experience)
summary(model1c_p1)

## What part of the variation is explained by the simultaneous vote?
model1d_p1 <- lm(data_partial_p$Pro ~ data_partial_p$Extreme + data_partial_p$Scale + data_partial_p$Experience + data_partial_p$CantonalLeft)
summary(model1d_p1)

# Making the table
cant_models_p <- list(model1a_p1, model1b_p1, model1c_p1, model1d_p1)

cm2 <- c('data_partial_p$Extreme'    = 'Simultaneous extreme, $E_{Sim}$ \\\\ (baseline: non-simultaneous)',
         'data_partial_p$Scale'    = 'Inclusiveness (range: 1-6)',
         'data_partial_p$Experience' = 'Prior experience',
         'data_partial_p$CantonalLeft' = 'Cantonal left')

table2 <- modelsummary(cant_models_p,  output = 'kableExtra',
                       fmt = 1, 
                       stars = c('*' = .1, '**' = .05, '***' = .01),
                       coef_omit = "Intercept",
                       gof_map = c("nobs"),
                       coef_map = cm2,
                       escape = FALSE,
                       title = 'Simultaneous Votes in Swiss Referendums (Robustness)')
table2

###############################################################################

# PART TWO: ANALYSES/RESULTS INCLUDED IN THE APPENDIX

# ANALYSIS OF CANTONAL DATA (MAIN HYPOTHESIS)--FULL DATASET.

# All analyses here are run with the full dataset.

## What part of the variation is explained by the simultaneous vote?
# random effects, no controls, full dataset
model1a_full <- lm(data$Pro ~ data$Simultaneous)
summary(model1a_full)

## What part of the variation is explained by the simultaneous vote?
# random effects, no controls, full dataset, added inclusion control
model1b_full <- lm(data$Pro ~ data$Simultaneous + data$Scale)
summary(model1b_full)

## What part of the variation is explained by the simultaneous vote?
# random effects, no controls, full dataset
model1c_full <- lm(data$Pro ~ data$Simultaneous + data$Scale + data$Experience)
summary(model1c_full)

## What part of the variation is explained by the simultaneous vote?
model1d_full <- lm(data$Pro ~ data$Simultaneous + data$Scale + data$Experience + data$CantonalLeft)
summary(model1d_full)

# Making the table 
cant_models_h_full <- list(model1a_full, model1b_full, model1c_full, model1d_full)

cm3_full <- c('data$Simultaneous'    = 'Simultaneous moderate, $M_{Sim}$ \\\\ (baseline: non-simultaneous and extreme)',
         'data$Scale'    = 'Inclusiveness (range: 1-6)',
         'data$Experience' = 'Prior experience',
         'data$CantonalLeft' = 'Cantonal left')

table3_full <- modelsummary(cant_models_h_full, output = 'kableExtra',
                       fmt = 1, 
                       stars = c('*' = .1, '**' = .05, '***' = .01),
                       coef_omit = "Intercept",
                       coef_map = cm3_full,
                       gof_map = c("nobs"),
                       escape = FALSE,
                       title = 'Simultaneous Votes in Swiss Referendums (Hypothesis 1)')
table3_full

###

# LEAVE-ONE-OUT SENSITIVITY ANALYSIS

n <- nrow(data_partial)
results_model1a <- list()
results_model1b <- list()
results_model1c <- list()
results_model1d <- list()

# using a for loop for leave-one-out analysis
for (i in 1:n) {
  data_loop <- data_partial[-i, ]
  
  model1a_loop <- lm(Pro ~ Simultaneous, data = data_loop)
  model1b_loop <- lm(Pro ~ Simultaneous + Scale, data = data_loop)
  model1c_loop <- lm(Pro ~ Simultaneous + Scale + Experience, data = data_loop)
  model1d_loop <- lm(Pro ~ Simultaneous + Scale + Experience + CantonalLeft, data = data_loop)

  results_model1a[[i]] <- summary(model1a_loop)$coefficients
  results_model1b[[i]] <- summary(model1b_loop)$coefficients
  results_model1c[[i]] <- summary(model1c_loop)$coefficients
  results_model1d[[i]] <- summary(model1d_loop)$coefficients
}

# Results
options(scipen = 999)
results_model1a
results_model1b
results_model1c
results_model1d
options(scipen = 0)

# Initialize lists to store results for all models
results_list <- list()

# Loop over each model's results (model1a, model1b, model1c, model1d)
for (model in list(results_model1a, results_model1b, results_model1c, results_model1d)) {
  # Extract the Simultaneous coefficients and p-values for all iterations
  coefficients <- sapply(model, function(x) return(x["Simultaneous", "Estimate"]))
  p_values <- sapply(model, function(x) return(x["Simultaneous", "Pr(>|t|)"]))
  
  # Combine into a dataframe
  model_results <- data.frame(
    Iteration = 1:length(coefficients),
    Simultaneous_Coeff = coefficients,
    Simultaneous_p_value = p_values
  )
  
  # Append results to list
  results_list <- append(results_list, list(model_results))
}

# Combine the results into one large table, labeling which model they correspond to
results_all_models <- do.call(rbind, Map(cbind, Model = c("Model 1", "Model 2", "Model 3", "Model 4"), results_list))

# Display the results as a formatted table
kable(results_all_models, format = "html", caption = "Simultaneous Coefficient and P-values for Leave-One-Out Iterations") %>%
  kable_styling(full_width = FALSE)

###############################################################################
# End. Thank you for reading.
###############################################################################